package com.mtt.erp.logistics.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.mtt.erp.common.annotation.Log;
import com.mtt.erp.common.api.R;
import com.mtt.erp.common.api.ResultCode;
import com.mtt.erp.common.domain.QueryRequest;
import com.mtt.erp.common.exception.MttErpException;
import com.mtt.erp.logistics.dto.FaceOrderPerson;
import com.mtt.erp.logistics.dto.OrderSenderInfoCreateDto;
import com.mtt.erp.logistics.dto.OrderSenderInfoDto;
import com.mtt.erp.logistics.dto.OrderSenderInfoUpdateDto;
import com.mtt.erp.logistics.service.IOrderSenderInfoService;
import com.mtt.erp.logistics.vo.OrderSenderInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * @author MrBird
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/orderSenderInfo")
@Api(value = "发货人模板", tags = "M007-物流-发货人模板接口")
public class OrderSenderInfoController {
    @Autowired
    private IOrderSenderInfoService iOrderSenderInfoService;

    /**
     * 根据查询条件获取订单发货信息列表
     *
     * @param request
     * @param orderSenderInfoDto
     * @return
     */
    //@Log("查询订单")
    @GetMapping
    @ApiOperation(value = "发货人列表", notes = "根据条件查询发货人列表,orderSenderInfoDto", position = 1)
    public R<IPage<OrderSenderInfoVo>> orderSenderInfoList(QueryRequest request, OrderSenderInfoDto orderSenderInfoDto) {
        IPage<OrderSenderInfoVo> orderSenderInfoIpage = this.iOrderSenderInfoService.getOrderSenderInfo(request, orderSenderInfoDto);
        if (orderSenderInfoIpage.getTotal() >0) {
            return R.data(orderSenderInfoIpage);
        } else {
            return R.fail(ResultCode.NO_CONTENT);
        }
    }

    /**
     * @param orderSenderInfoCreateDto
     * @throws MttErpException
     */
    @PostMapping
    @ApiOperation(value = "新增发货人信息", notes = "将发货人信息保存到数据库,orderSenderInfoCreateDto", position = 2)
    public R createOrderSenderInfo(@Valid OrderSenderInfoCreateDto orderSenderInfoCreateDto){
        try {
            this.iOrderSenderInfoService.createOrderSenderInfo(orderSenderInfoCreateDto);
            return R.success(ResultCode.SUCCESS);
        } catch (Exception e) {
           return R.fail(ResultCode.FAILURE);
        }
    }

    /**
     * @param orderSenderInfoUpdateDto
     * @throws MttErpException
     */
    @PutMapping
    @ApiOperation(value = "修改发货人信息", notes = "根据发货人id修改发货人信息,orderSenderInfoUpdateDto", position = 3)
    public R updateOrderSenderInfo(@Valid OrderSenderInfoUpdateDto orderSenderInfoUpdateDto) {
        try {
            this.iOrderSenderInfoService.updateOrderSenderInfo(orderSenderInfoUpdateDto);
            return R.success(ResultCode.SUCCESS);
        } catch (Exception e) {
           return R.fail(ResultCode.FAILURE);
        }
    }

    @DeleteMapping("/{orderSenderInfoIds}")
    @ApiOperation(value = "删除发货人信息", notes = "批量删除发货人信息,传入要删除的发货人信息id串,以逗号分隔", position = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(value="id串,以逗号分割",name="orderSenderInfoIds",required=true,paramType = "path")
    })
    public R deleteOrderSenderInfo(@NotBlank(message = "{required}") @PathVariable String orderSenderInfoIds){
        try {
            String[] ids = orderSenderInfoIds.split(StringPool.COMMA);
            this.iOrderSenderInfoService.deleteOrderSenderInfo(ids);
            return R.success(ResultCode.SUCCESS);
        } catch (Exception e) {
           return R.fail(ResultCode.FAILURE);
        }
    }

    /**
     * 商品类型对象
     *
     * @author guomengzhou
     */
    @Log("查询发货人对象信息")
    @GetMapping("/{id}")
    @ApiOperation(value = "查询发货人对象信息", notes = "根据类型id查询类型信息", position = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "发货人id", required=true,paramType = "path")
    })
    public R<OrderSenderInfoVo> getCategory(@NotBlank(message = "{required}") @PathVariable String id) {
        OrderSenderInfoVo orderSenderInfoVo = this.iOrderSenderInfoService.getOrderSenderInfoById(id);
        if (orderSenderInfoVo != null) {
            return R.data(orderSenderInfoVo);
        }else{
            return R.fail(ResultCode.NO_CONTENT);
        }
    }
}
